package org.casaca.checkbackup.core.checksum;

import java.util.zip.CRC32;

import org.casaca.checkbackup.core.exception.ChecksumInitializeException;

public class CRC32Checksum extends Checksum {
	
	private java.util.zip.Checksum chk;
	private String checksum;

	public CRC32Checksum(String name, String shortname) throws ChecksumInitializeException {
		super(name, shortname);
		
		this.chk = new CRC32();
		this.checksum = null;
	}

	@Override
	public void addSlice(byte[] b, int offset, int len) {
		this.chk.update(b, offset, len);
	}

	@Override
	public void complete() {
		this.checksum = String.valueOf(this.chk.getValue());
	}

	@Override
	public String getChecksum() {
		if(this.checksum==null)
			throw new IllegalStateException("Complete the process before getting the checksum");
		
		return this.checksum;
	}

	@Override
	public void reset() {
		this.chk = new CRC32();
		this.checksum = null;
	}

	@Override
	public String getChecksumType() {
		return "CRC32";
	}
}